home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / sun4.md / gdb / RCS / solib.c,v < prev    next >
Encoding:
Text File  |  1992-06-24  |  33.2 KB  |  1,278 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     92.06.09.14.26.47;  author secor;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
  26.    Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
  27.    
  28. This file is part of GDB.
  29.  
  30. This program is free software; you can redistribute it and/or modify
  31. it under the terms of the GNU General Public License as published by
  32. the Free Software Foundation; either version 2 of the License, or
  33. (at your option) any later version.
  34.  
  35. This program is distributed in the hope that it will be useful,
  36. but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  38. GNU General Public License for more details.
  39.  
  40. You should have received a copy of the GNU General Public License
  41. along with this program; if not, write to the Free Software
  42. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  43.  
  44.  
  45. #include "defs.h"
  46.  
  47. #include <sys/types.h>
  48. #include <signal.h>
  49. #include <string.h>
  50. #include "link.h"
  51. #include <sys/param.h>
  52. #include <fcntl.h>
  53.  
  54. #ifndef SVR4_SHARED_LIBS
  55.  /* SunOS shared libs need the nlist structure.  */
  56. #include <a.out.h> 
  57. #endif
  58.  
  59. #include "symtab.h"
  60. #include "bfd.h"
  61. #include "symfile.h"
  62. #include "objfiles.h"
  63. #include "gdbcore.h"
  64. #include "command.h"
  65. #include "target.h"
  66. #include "frame.h"
  67. #include "regex.h"
  68. #include "inferior.h"
  69.  
  70. #define MAX_PATH_SIZE 256        /* FIXME: Should be dynamic */
  71.  
  72. /* On SVR4 systems, for the initial implementation, use main() as the
  73.    "startup mapping complete" breakpoint address.  The models for SunOS
  74.    and SVR4 dynamic linking debugger support are different in that SunOS
  75.    hits one breakpoint when all mapping is complete while using the SVR4
  76.    debugger support takes two breakpoint hits for each file mapped, and
  77.    there is no way to know when the "last" one is hit.  Both these
  78.    mechanisms should be tied to a "breakpoint service routine" that
  79.    gets automatically executed whenever one of the breakpoints indicating
  80.    a change in mapping is hit.  This is a future enhancement.  (FIXME) */
  81.  
  82. #define BKPT_AT_MAIN 1
  83.  
  84. /* local data declarations */
  85.  
  86. #ifndef SVR4_SHARED_LIBS
  87.  
  88. #define DEBUG_BASE "_DYNAMIC"
  89. #define LM_ADDR(so) ((so) -> lm.lm_addr)
  90. #define LM_NEXT(so) ((so) -> lm.lm_next)
  91. #define LM_NAME(so) ((so) -> lm.lm_name)
  92. static struct link_dynamic dynamic_copy;
  93. static struct link_dynamic_2 ld_2_copy;
  94. static struct ld_debug debug_copy;
  95. static CORE_ADDR debug_addr;
  96. static CORE_ADDR flag_addr;
  97.  
  98. #else    /* SVR4_SHARED_LIBS */
  99.  
  100. #define DEBUG_BASE "_r_debug"
  101. #define LM_ADDR(so) ((so) -> lm.l_addr)
  102. #define LM_NEXT(so) ((so) -> lm.l_next)
  103. #define LM_NAME(so) ((so) -> lm.l_name)
  104. static struct r_debug debug_copy;
  105. char shadow_contents[BREAKPOINT_MAX];    /* Stash old bkpt addr contents */
  106.  
  107. #endif    /* !SVR4_SHARED_LIBS */
  108.  
  109. struct so_list {
  110.   struct so_list *next;            /* next structure in linked list */
  111.   struct link_map lm;            /* copy of link map from inferior */
  112.   struct link_map *lmaddr;        /* addr in inferior lm was read from */
  113.   CORE_ADDR lmend;            /* upper addr bound of mapped object */
  114.   char so_name[MAX_PATH_SIZE];        /* shared object lib name (FIXME) */
  115.   char symbols_loaded;            /* flag: symbols read in yet? */
  116.   char from_tty;            /* flag: print msgs? */
  117.   bfd *so_bfd;                /* bfd for so_name */
  118.   struct objfile *objfile;        /* objfile for loaded lib */
  119.   struct section_table *sections;
  120.   struct section_table *sections_end;
  121.   struct section_table *textsection;
  122. };
  123.  
  124. static struct so_list *so_list_head;    /* List of known shared objects */
  125. static CORE_ADDR debug_base;        /* Base of dynamic linker structures */
  126. static CORE_ADDR breakpoint_addr;    /* Address where end bkpt is set */
  127.  
  128. /* Local function prototypes */
  129.  
  130. static void
  131. special_symbol_handling PARAMS ((struct so_list *));
  132.  
  133. static void
  134. sharedlibrary_command PARAMS ((char *, int));
  135.  
  136. static int
  137. enable_break PARAMS ((void));
  138.  
  139. static int
  140. disable_break PARAMS ((void));
  141.  
  142. static void
  143. info_sharedlibrary_command PARAMS ((char *, int));
  144.  
  145. static int
  146. symbol_add_stub PARAMS ((char *));
  147.  
  148. static struct so_list *
  149. find_solib PARAMS ((struct so_list *));
  150.  
  151. static struct link_map *
  152. first_link_map_member PARAMS ((void));
  153.  
  154. static CORE_ADDR
  155. locate_base PARAMS ((void));
  156.  
  157. static void
  158. solib_map_sections PARAMS ((struct so_list *));
  159.  
  160. #ifdef SVR4_SHARED_LIBS
  161.  
  162. static int
  163. look_for_base PARAMS ((int, CORE_ADDR));
  164.  
  165. static CORE_ADDR
  166. bfd_lookup_symbol PARAMS ((bfd *, char *));
  167.  
  168. #else
  169.  
  170. static void
  171. solib_add_common_symbols PARAMS ((struct rtc_symb *, struct objfile *));
  172.  
  173. #endif
  174.  
  175. /*
  176.  
  177. LOCAL FUNCTION
  178.  
  179.     solib_map_sections -- open bfd and build sections for shared lib
  180.  
  181. SYNOPSIS
  182.  
  183.     static void solib_map_sections (struct so_list *so)
  184.  
  185. DESCRIPTION
  186.  
  187.     Given a pointer to one of the shared objects in our list
  188.     of mapped objects, use the recorded name to open a bfd
  189.     descriptor for the object, build a section table, and then
  190.     relocate all the section addresses by the base address at
  191.     which the shared object was mapped.
  192.  
  193. FIXMES
  194.  
  195.     In most (all?) cases the shared object file name recorded in the
  196.     dynamic linkage tables will be a fully qualified pathname.  For
  197.     cases where it isn't, do we really mimic the systems search
  198.     mechanism correctly in the below code (particularly the tilde
  199.     expansion stuff?).
  200.  */
  201.  
  202. static void
  203. solib_map_sections (so)
  204.      struct so_list *so;
  205. {
  206.   char *filename;
  207.   char *scratch_pathname;
  208.   int scratch_chan;
  209.   struct section_table *p;
  210.   
  211.   filename = tilde_expand (so -> so_name);
  212.   make_cleanup (free, filename);
  213.   
  214.   scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  215.             &scratch_pathname);
  216.   if (scratch_chan < 0)
  217.     {
  218.       scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
  219.                 O_RDONLY, 0, &scratch_pathname);
  220.     }
  221.   if (scratch_chan < 0)
  222.     {
  223.       perror_with_name (filename);
  224.     }  
  225.  
  226.   so -> so_bfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan);
  227.   if (!so -> so_bfd)
  228.     {
  229.       error ("Could not open `%s' as an executable file: %s",
  230.          scratch_pathname, bfd_errmsg (bfd_error));
  231.     }
  232.   if (!bfd_check_format (so -> so_bfd, bfd_object))
  233.     {
  234.       error ("\"%s\": not in executable format: %s.",
  235.          scratch_pathname, bfd_errmsg (bfd_error));
  236.     }
  237.   if (build_section_table (so -> so_bfd, &so -> sections, &so -> sections_end))
  238.     {
  239.       error ("Can't find the file sections in `%s': %s", 
  240.          exec_bfd -> filename, bfd_errmsg (bfd_error));
  241.     }
  242.  
  243.   for (p = so -> sections; p < so -> sections_end; p++)
  244.     {
  245.       /* Relocate the section binding addresses as recorded in the shared
  246.      object's file by the base address to which the object was actually
  247.      mapped. */
  248.       p -> addr += (CORE_ADDR) LM_ADDR (so);
  249.       p -> endaddr += (CORE_ADDR) LM_ADDR (so);
  250.       so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
  251.       if (strcmp (p -> sec_ptr -> name, ".text") == 0)
  252.     {
  253.       so -> textsection = p;
  254.     }
  255.     }
  256. }
  257.  
  258. /* Read all dynamically loaded common symbol definitions from the inferior
  259.    and add them to the minimal symbol table for the shared library objfile.  */
  260.  
  261. #ifndef SVR4_SHARED_LIBS
  262.  
  263. static void
  264. solib_add_common_symbols (rtc_symp, objfile)
  265.     struct rtc_symb *rtc_symp;
  266.     struct objfile *objfile;
  267. {
  268.   struct rtc_symb inferior_rtc_symb;
  269.   struct nlist inferior_rtc_nlist;
  270.   int len;
  271.   char *name;
  272.   char *origname;
  273.  
  274.   init_minimal_symbol_collection ();
  275.   make_cleanup (discard_minimal_symbols, 0);
  276.  
  277.   while (rtc_symp)
  278.     {
  279.       read_memory ((CORE_ADDR) rtc_symp,
  280.            (char *) &inferior_rtc_symb,
  281.            sizeof (inferior_rtc_symb));
  282.       read_memory ((CORE_ADDR) inferior_rtc_symb.rtc_sp,
  283.            (char *) &inferior_rtc_nlist,
  284.            sizeof(inferior_rtc_nlist));
  285.       if (inferior_rtc_nlist.n_type == N_COMM)
  286.     {
  287.       /* FIXME: The length of the symbol name is not available, but in the
  288.          current implementation the common symbol is allocated immediately
  289.          behind the name of the symbol. */
  290.       len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
  291.  
  292.       origname = name = xmalloc (len);
  293.       read_memory ((CORE_ADDR) inferior_rtc_nlist.n_un.n_name, name, len);
  294.  
  295.       /* Don't enter the symbol twice if the target is re-run. */
  296.  
  297. #ifdef NAMES_HAVE_UNDERSCORE
  298.       if (*name == '_')
  299.         {
  300.           name++;
  301.         }
  302. #endif
  303.       /* FIXME:  Do we really want to exclude symbols which happen
  304.          to match symbols for other locations in the inferior's
  305.          address space, even when they are in different linkage units? */
  306.       if (lookup_minimal_symbol (name, (struct objfile *) NULL) == NULL)
  307.         {
  308.           name = obsavestring (name, strlen (name),
  309.                    &objfile -> symbol_obstack);
  310.           prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
  311.                       mst_bss);
  312.         }
  313.       free (origname);
  314.     }
  315.       rtc_symp = inferior_rtc_symb.rtc_next;
  316.     }
  317.  
  318.   /* Install any minimal symbols that have been collected as the current
  319.      minimal symbols for this objfile. */
  320.  
  321.   install_minimal_symbols (objfile);
  322. }
  323.  
  324. #endif    /* SVR4_SHARED_LIBS */
  325.  
  326. #ifdef SVR4_SHARED_LIBS
  327.  
  328. /*
  329.  
  330. LOCAL FUNCTION
  331.  
  332.     bfd_lookup_symbol -- lookup the value for a specific symbol
  333.  
  334. SYNOPSIS
  335.  
  336.     CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
  337.  
  338. DESCRIPTION
  339.  
  340.     An expensive way to lookup the value of a single symbol for
  341.     bfd's that are only temporary anyway.  This is used by the
  342.     shared library support to find the address of the debugger
  343.     interface structures in the shared library.
  344.  
  345.     Note that 0 is specifically allowed as an error return (no
  346.     such symbol).
  347.  
  348.     FIXME:  See if there is a less "expensive" way of doing this.
  349.     Also see if there is already another bfd or gdb function
  350.     that specifically does this, and if so, use it.
  351. */
  352.  
  353. static CORE_ADDR
  354. bfd_lookup_symbol (abfd, symname)
  355.      bfd *abfd;
  356.      char *symname;
  357. {
  358.   unsigned int storage_needed;
  359.   asymbol *sym;
  360.   asymbol **symbol_table;
  361.   unsigned int number_of_symbols;
  362.   unsigned int i;
  363.   struct cleanup *back_to;
  364.   CORE_ADDR symaddr = 0;
  365.   
  366.   storage_needed = get_symtab_upper_bound (abfd);
  367.  
  368.   if (storage_needed > 0)
  369.     {
  370.       symbol_table = (asymbol **) xmalloc (storage_needed);
  371.       back_to = make_cleanup (free, (PTR)symbol_table);
  372.       number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); 
  373.   
  374.       for (i = 0; i < number_of_symbols; i++)
  375.     {
  376.       sym = *symbol_table++;
  377.       if (strcmp (sym -> name, symname) == 0)
  378.         {
  379.           symaddr = sym -> value;
  380.           break;
  381.         }
  382.     }
  383.       do_cleanups (back_to);
  384.     }
  385.   return (symaddr);
  386. }
  387.  
  388. /*
  389.  
  390. LOCAL FUNCTION
  391.  
  392.     look_for_base -- examine file for each mapped address segment
  393.  
  394. SYNOPSYS
  395.  
  396.     static int look_for_base (int fd, CORE_ADDR baseaddr)
  397.  
  398. DESCRIPTION
  399.  
  400.     This function is passed to proc_iterate_over_mappings, which
  401.     causes it to get called once for each mapped address space, with
  402.     an open file descriptor for the file mapped to that space, and the
  403.     base address of that mapped space.
  404.  
  405.     Our job is to find the symbol DEBUG_BASE in the file that this
  406.     fd is open on, if it exists, and if so, initialize the dynamic
  407.     linker structure base address debug_base.
  408.  
  409.     Note that this is a computationally expensive proposition, since
  410.     we basically have to open a bfd on every call, so we specifically
  411.     avoid opening the exec file.
  412.  */
  413.  
  414. static int
  415. look_for_base (fd, baseaddr)
  416.      int fd;
  417.      CORE_ADDR baseaddr;
  418. {
  419.   bfd *interp_bfd;
  420.   CORE_ADDR address;
  421.  
  422.   /* If the fd is -1, then there is no file that corresponds to this
  423.      mapped memory segment, so skip it.  Also, if the fd corresponds
  424.      to the exec file, skip it as well. */
  425.  
  426.   if ((fd == -1) || fdmatch (fileno ((FILE *)(exec_bfd -> iostream)), fd))
  427.     {
  428.       return (0);
  429.     }
  430.  
  431.   /* Try to open whatever random file this fd corresponds to.  Note that
  432.      we have no way currently to find the filename.  Don't gripe about
  433.      any problems we might have, just fail. */
  434.  
  435.   if ((interp_bfd = bfd_fdopenr ("unnamed", NULL, fd)) == NULL)
  436.     {
  437.       return (0);
  438.     }
  439.   if (!bfd_check_format (interp_bfd, bfd_object))
  440.     {
  441.       bfd_close (interp_bfd);
  442.       return (0);
  443.     }
  444.  
  445.   /* Now try to find our DEBUG_BASE symbol in this file, which we at
  446.      least know to be a valid ELF executable or shared library. */
  447.  
  448.   if ((address = bfd_lookup_symbol (interp_bfd, DEBUG_BASE)) == 0)
  449.     {
  450.       bfd_close (interp_bfd);
  451.       return (0);
  452.     }
  453.  
  454.   /* Eureka!  We found the symbol.  But now we may need to relocate it
  455.      by the base address.  If the symbol's value is less than the base
  456.      address of the shared library, then it hasn't yet been relocated
  457.      by the dynamic linker, and we have to do it ourself.  FIXME: Note
  458.      that we make the assumption that the first segment that corresponds
  459.      to the shared library has the base address to which the library
  460.      was relocated. */
  461.  
  462.   if (address < baseaddr)
  463.     {
  464.       address += baseaddr;
  465.     }
  466.   debug_base = address;
  467.   bfd_close (interp_bfd);
  468.   return (1);
  469. }
  470.  
  471. #endif
  472.  
  473. /*
  474.  
  475. LOCAL FUNCTION
  476.  
  477.     locate_base -- locate the base address of dynamic linker structs
  478.  
  479. SYNOPSIS
  480.  
  481.     CORE_ADDR locate_base (void)
  482.  
  483. DESCRIPTION
  484.  
  485.     For both the SunOS and SVR4 shared library implementations, if the
  486.     inferior executable has been linked dynamically, there is a single
  487.     address somewhere in the inferior's data space which is the key to
  488.     locating all of the dynamic linker's runtime structures.  This
  489.     address is the value of the symbol defined by the macro DEBUG_BASE.
  490.     The job of this function is to find and return that address, or to
  491.     return 0 if there is no such address (the executable is statically
  492.     linked for example).
  493.  
  494.     For SunOS, the job is almost trivial, since the dynamic linker and
  495.     all of it's structures are statically linked to the executable at
  496.     link time.  Thus the symbol for the address we are looking for has
  497.     already been added to the minimal symbol table for the executable's
  498.     objfile at the time the symbol file's symbols were read, and all we
  499.     have to do is look it up there.  Note that we explicitly do NOT want
  500.     to find the copies in the shared library.
  501.  
  502.     The SVR4 version is much more complicated because the dynamic linker
  503.     and it's structures are located in the shared C library, which gets
  504.     run as the executable's "interpreter" by the kernel.  We have to go
  505.     to a lot more work to discover the address of DEBUG_BASE.  Because
  506.     of this complexity, we cache the value we find and return that value
  507.     on subsequent invocations.  Note there is no copy in the executable
  508.     symbol tables.
  509.  
  510.     Note that we can assume nothing about the process state at the time
  511.     we need to find this address.  We may be stopped on the first instruc-
  512.     tion of the interpreter (C shared library), the first instruction of
  513.     the executable itself, or somewhere else entirely (if we attached
  514.     to the process for example).
  515.  
  516.  */
  517.  
  518. static CORE_ADDR
  519. locate_base ()
  520. {
  521.  
  522. #ifndef SVR4_SHARED_LIBS
  523.  
  524.   struct minimal_symbol *msymbol;
  525.   CORE_ADDR address = 0;
  526.  
  527.   /* For SunOS, we want to limit the search for DEBUG_BASE to the executable
  528.      being debugged, since there is a duplicate named symbol in the shared
  529.      library.  We don't want the shared library versions. */
  530.  
  531.   msymbol = lookup_minimal_symbol (DEBUG_BASE, symfile_objfile);
  532.   if ((msymbol != NULL) && (msymbol -> address != 0))
  533.     {
  534.       address = msymbol -> address;
  535.     }
  536.   return (address);
  537.  
  538. #else    /* SVR4_SHARED_LIBS */
  539.  
  540.   /* Check to see if we have a currently valid address, and if so, avoid
  541.      doing all this work again and just return the cached address.  If
  542.      we have no cached address, ask the /proc support interface to iterate
  543.      over the list of mapped address segments, calling look_for_base() for
  544.      each segment.  When we are done, we will have either found the base
  545.      address or not. */
  546.  
  547.   if (debug_base == 0)
  548.     {
  549.       proc_iterate_over_mappings (look_for_base);
  550.     }
  551.   return (debug_base);
  552.  
  553. #endif    /* !SVR4_SHARED_LIBS */
  554.  
  555. }
  556.  
  557. static struct link_map *
  558. first_link_map_member ()
  559. {
  560.   struct link_map *lm = NULL;
  561.  
  562. #ifndef SVR4_SHARED_LIBS
  563.  
  564.   read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
  565.   if (dynamic_copy.ld_version >= 2)
  566.     {
  567.       /* It is a version that we can deal with, so read in the secondary
  568.      structure and find the address of the link map list from it. */
  569.       read_memory ((CORE_ADDR) dynamic_copy.ld_un.ld_2, (char *) &ld_2_copy,
  570.            sizeof (struct link_dynamic_2));
  571.       lm = ld_2_copy.ld_loaded;
  572.     }
  573.  
  574. #else    /* SVR4_SHARED_LIBS */
  575.  
  576.   read_memory (debug_base, (char *) &debug_copy, sizeof (struct r_debug));
  577.   lm = debug_copy.r_map;
  578.  
  579. #endif    /* !SVR4_SHARED_LIBS */
  580.  
  581.   return (lm);
  582. }
  583.  
  584. /*
  585.  
  586. LOCAL FUNCTION
  587.  
  588.     find_solib -- step through list of shared objects
  589.  
  590. SYNOPSIS
  591.  
  592.     struct so_list *find_solib (struct so_list *so_list_ptr)
  593.  
  594. DESCRIPTION
  595.  
  596.     This module contains the routine which finds the names of any
  597.     loaded "images" in the current process. The argument in must be
  598.     NULL on the first call, and then the returned value must be passed
  599.     in on subsequent calls. This provides the capability to "step" down
  600.     the list of loaded objects. On the last object, a NULL value is
  601.     returned.
  602.  
  603.     The arg and return value are "struct link_map" pointers, as defined
  604.     in <link.h>.
  605.  */
  606.  
  607. static struct so_list *
  608. find_solib (so_list_ptr)
  609.      struct so_list *so_list_ptr;    /* Last lm or NULL for first one */
  610. {
  611.   struct so_list *so_list_next = NULL;
  612.   struct link_map *lm = NULL;
  613.   struct so_list *new;
  614.   
  615.   if (so_list_ptr == NULL)
  616.     {
  617.       /* We are setting up for a new scan through the loaded images. */
  618.       if ((so_list_next = so_list_head) == NULL)
  619.     {
  620.       /* We have not already read in the dynamic linking structures
  621.          from the inferior, lookup the address of the base structure. */
  622.       debug_base = locate_base ();
  623.       if (debug_base > 0)
  624.         {
  625.           /* Read the base structure in and find the address of the first
  626.          link map list member. */
  627.           lm = first_link_map_member ();
  628.         }
  629.     }
  630.     }
  631.   else
  632.     {
  633.       /* We have been called before, and are in the process of walking
  634.      the shared library list.  Advance to the next shared object. */
  635.       if ((lm = LM_NEXT (so_list_ptr)) == NULL)
  636.     {
  637.       /* We have hit the end of the list, so check to see if any were
  638.          added, but be quiet if we can't read from the target any more. */
  639.       int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lmaddr,
  640.                        (char *) &(so_list_ptr -> lm),
  641.                        sizeof (struct link_map));
  642.       if (status == 0)
  643.         {
  644.           lm = LM_NEXT (so_list_ptr);
  645.         }
  646.       else
  647.         {
  648.           lm = NULL;
  649.         }
  650.     }
  651.       so_list_next = so_list_ptr -> next;
  652.     }
  653.   if ((so_list_next == NULL) && (lm != NULL))
  654.     {
  655.       /* Get next link map structure from inferior image and build a local
  656.      abbreviated load_map structure */
  657.       new = (struct so_list *) xmalloc (sizeof (struct so_list));
  658.       (void) memset ((char *) new, 0, sizeof (struct so_list));
  659.       new -> lmaddr = lm;
  660.       /* Add the new node as the next node in the list, or as the root
  661.      node if this is the first one. */
  662.       if (so_list_ptr != NULL)
  663.     {
  664.       so_list_ptr -> next = new;
  665.     }
  666.       else
  667.     {
  668.       so_list_head = new;
  669.     }      
  670.       so_list_next = new;
  671.       read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
  672.            sizeof (struct link_map));
  673.       /* For the SVR4 version, there is one entry that has no name
  674.      (for the inferior executable) since it is not a shared object. */
  675.       if (LM_NAME (new) != 0)
  676.     {
  677.       if (!target_read_string((CORE_ADDR) LM_NAME (new), new -> so_name,
  678.               MAX_PATH_SIZE - 1))
  679.           error ("find_solib: Can't read pathname for load map\n");
  680.       new -> so_name[MAX_PATH_SIZE - 1] = 0;
  681.       solib_map_sections (new);
  682.     }      
  683.     }
  684.   return (so_list_next);
  685. }
  686.  
  687. /* A small stub to get us past the arg-passing pinhole of catch_errors.  */
  688.  
  689. static int
  690. symbol_add_stub (arg)
  691.      char *arg;
  692. {
  693.   register struct so_list *so = (struct so_list *) arg;    /* catch_errs bogon */
  694.   
  695.   so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
  696.                    (unsigned int) so -> textsection -> addr,
  697.                    0, 0, 0);
  698.   return (1);
  699. }
  700.  
  701. /*
  702.  
  703. GLOBAL FUNCTION
  704.  
  705.     solib_add -- add a shared library file to the symtab and section list
  706.  
  707. SYNOPSIS
  708.  
  709.     void solib_add (char *arg_string, int from_tty,
  710.             struct target_ops *target)
  711.  
  712. DESCRIPTION
  713.  
  714. */
  715.  
  716. void
  717. solib_add (arg_string, from_tty, target)
  718.      char *arg_string;
  719.      int from_tty;
  720.      struct target_ops *target;
  721. {    
  722.   register struct so_list *so = NULL;       /* link map state variable */
  723.   char *re_err;
  724.   int count;
  725.   int old;
  726.   
  727.   if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
  728.     {
  729.       error ("Invalid regexp: %s", re_err);
  730.     }
  731.   
  732.   /* Getting new symbols may change our opinion about what is
  733.      frameless.  */
  734.   reinit_frame_cache ();
  735.   
  736.   while ((so = find_solib (so)) != NULL)
  737.     {
  738.       if (so -> so_name[0] && re_exec (so -> so_name))
  739.     {
  740.       if (so -> symbols_loaded)
  741.         {
  742.           if (from_tty)
  743.         {
  744.           printf ("Symbols already loaded for %s\n", so -> so_name);
  745.         }
  746.         }
  747.       else
  748.         {
  749.           catch_errors (symbol_add_stub, (char *) so,
  750.                 "Error while reading shared library symbols:\n");
  751.           
  752.           special_symbol_handling (so);
  753.           so -> symbols_loaded = 1;
  754.           so -> from_tty = from_tty;
  755.         }
  756.     }
  757.     }
  758.   
  759.   /* Now add the shared library sections to the section table of the
  760.      specified target, if any.  */
  761.   if (target)
  762.     {
  763.       /* Count how many new section_table entries there are.  */
  764.       so = NULL;
  765.       count = 0;
  766.       while ((so = find_solib (so)) != NULL)
  767.     {
  768.       if (so -> so_name[0])
  769.         {
  770.           count += so -> sections_end - so -> sections;
  771.         }
  772.     }
  773.       
  774.       if (count)
  775.     {
  776.       /* Reallocate the target's section table including the new size.  */
  777.       if (target -> to_sections)
  778.         {
  779.           old = target -> to_sections_end - target -> to_sections;
  780.           target -> to_sections = (struct section_table *)
  781.         realloc ((char *)target -> to_sections,
  782.              (sizeof (struct section_table)) * (count + old));
  783.         }
  784.       else
  785.         {
  786.           old = 0;
  787.           target -> to_sections = (struct section_table *)
  788.         malloc ((sizeof (struct section_table)) * count);
  789.         }
  790.       target -> to_sections_end = target -> to_sections + (count + old);
  791.       
  792.       /* Add these section table entries to the target's table.  */
  793.       while ((so = find_solib (so)) != NULL)
  794.         {
  795.           if (so -> so_name[0])
  796.         {
  797.           count = so -> sections_end - so -> sections;
  798.           bcopy (so -> sections, (char *)(target -> to_sections + old), 
  799.              (sizeof (struct section_table)) * count);
  800.           old += count;
  801.         }
  802.         }
  803.     }
  804.     }
  805. }
  806.  
  807. /*
  808.  
  809. LOCAL FUNCTION
  810.  
  811.     info_sharedlibrary_command -- code for "info sharedlibrary"
  812.  
  813. SYNOPSIS
  814.  
  815.     static void info_sharedlibrary_command ()
  816.  
  817. DESCRIPTION
  818.  
  819.     Walk through the shared library list and print information
  820.     about each attached library.
  821. */
  822.  
  823. static void
  824. info_sharedlibrary_command (ignore, from_tty)
  825.      char *ignore;
  826.      int from_tty;
  827. {
  828.   register struct so_list *so = NULL;      /* link map state variable */
  829.   int header_done = 0;
  830.   
  831.   if (exec_bfd == NULL)
  832.     {
  833.       printf ("No exec file.\n");
  834.       return;
  835.     }
  836.   while ((so = find_solib (so)) != NULL)
  837.     {
  838.       if (so -> so_name[0])
  839.     {
  840.       if (!header_done)
  841.         {
  842.           printf("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
  843.              "Shared Object Library");
  844.           header_done++;
  845.         }
  846.       printf ("%-12s", local_hex_string_custom ((int) LM_ADDR (so), "08"));
  847.       printf ("%-12s", local_hex_string_custom (so -> lmend, "08"));
  848.       printf ("%-12s", so -> symbols_loaded ? "Yes" : "No");
  849.       printf ("%s\n",  so -> so_name);
  850.     }
  851.     }
  852.   if (so_list_head == NULL)
  853.     {
  854.       printf ("No shared libraries loaded at this time.\n");    
  855.     }
  856. }
  857.  
  858. /*
  859.  
  860. GLOBAL FUNCTION
  861.  
  862.     solib_address -- check to see if an address is in a shared lib
  863.  
  864. SYNOPSIS
  865.  
  866.     int solib_address (CORE_ADDR address)
  867.  
  868. DESCRIPTION
  869.  
  870.     Provides a hook for other gdb routines to discover whether or
  871.     not a particular address is within the mapped address space of
  872.     a shared library.  Any address between the base mapping address
  873.     and the first address beyond the end of the last mapping, is
  874.     considered to be within the shared library address space, for
  875.     our purposes.
  876.  
  877.     For example, this routine is called at one point to disable
  878.     breakpoints which are in shared libraries that are not currently
  879.     mapped in.
  880.  */
  881.  
  882. int
  883. solib_address (address)
  884.      CORE_ADDR address;
  885. {
  886.   register struct so_list *so = 0;       /* link map state variable */
  887.   
  888.   while ((so = find_solib (so)) != NULL)
  889.     {
  890.       if (so -> so_name[0])
  891.     {
  892.       if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
  893.           (address < (CORE_ADDR) so -> lmend))
  894.         {
  895.           return (1);
  896.         }
  897.     }
  898.     }
  899.   return (0);
  900. }
  901.  
  902. /* Called by free_all_symtabs */
  903.  
  904. void 
  905. clear_solib()
  906. {
  907.   struct so_list *next;
  908.   
  909.   while (so_list_head)
  910.     {
  911.       if (so_list_head -> sections)
  912.     {
  913.       free ((PTR)so_list_head -> sections);
  914.     }
  915.       if (so_list_head -> so_bfd)
  916.     {
  917.       bfd_close (so_list_head -> so_bfd);
  918.     }
  919.       next = so_list_head -> next;
  920.       free((PTR)so_list_head);
  921.       so_list_head = next;
  922.     }
  923.   debug_base = 0;
  924. }
  925.  
  926. /*
  927.  
  928. LOCAL FUNCTION
  929.  
  930.     disable_break -- remove the "mapping changed" breakpoint
  931.  
  932. SYNOPSIS
  933.  
  934.     static int disable_break ()
  935.  
  936. DESCRIPTION
  937.  
  938.     Removes the breakpoint that gets hit when the dynamic linker
  939.     completes a mapping change.
  940.  
  941. */
  942.  
  943. static int
  944. disable_break ()
  945. {
  946.   int status = 1;
  947.  
  948. #ifndef SVR4_SHARED_LIBS
  949.  
  950.   int in_debugger = 0;
  951.   
  952.   /* Read the debugger structure from the inferior to retrieve the
  953.      address of the breakpoint and the original contents of the
  954.      breakpoint address.  Remove the breakpoint by writing the original
  955.      contents back. */
  956.  
  957.   read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
  958.  
  959.   /* Set `in_debugger' to zero now. */
  960.  
  961.   write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
  962.  
  963.   breakpoint_addr = (CORE_ADDR) debug_copy.ldd_bp_addr;
  964.   write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
  965.         sizeof (debug_copy.ldd_bp_inst));
  966.  
  967. #else    /* SVR4_SHARED_LIBS */
  968.  
  969.   /* Note that breakpoint address and original contents are in our address
  970.      space, so we just need to write the original contents back. */
  971.  
  972.   if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
  973.     {
  974.       status = 0;
  975.     }
  976.  
  977. #endif    /* !SVR4_SHARED_LIBS */
  978.  
  979.   /* For the SVR4 version, we always know the breakpoint address.  For the
  980.      SunOS version we don't know it until the above code is executed.
  981.      Grumble if we are stopped anywhere besides the breakpoint address. */
  982.  
  983.   if (stop_pc != breakpoint_addr)
  984.     {
  985.       warning ("stopped at unknown breakpoint while handling shared libraries");
  986.     }
  987.  
  988.   return (status);
  989. }
  990.  
  991. /*
  992.  
  993. LOCAL FUNCTION
  994.  
  995.     enable_break -- arrange for dynamic linker to hit breakpoint
  996.  
  997. SYNOPSIS
  998.  
  999.     int enable_break (void)
  1000.  
  1001. DESCRIPTION
  1002.  
  1003.     Both the SunOS and the SVR4 dynamic linkers have, as part of their
  1004.     debugger interface, support for arranging for the inferior to hit
  1005.     a breakpoint after mapping in the shared libraries.  This function
  1006.     enables that breakpoint.
  1007.  
  1008.     For SunOS, there is a special flag location (in_debugger) which we
  1009.     set to 1.  When the dynamic linker sees this flag set, it will set
  1010.     a breakpoint at a location known only to itself, after saving the
  1011.     original contents of that place and the breakpoint address itself,
  1012.     in it's own internal structures.  When we resume the inferior, it
  1013.     will eventually take a SIGTRAP when it runs into the breakpoint.
  1014.     We handle this (in a different place) by restoring the contents of
  1015.     the breakpointed location (which is only known after it stops),
  1016.     chasing around to locate the shared libraries that have been
  1017.     loaded, then resuming.
  1018.  
  1019.     For SVR4, the debugger interface structure contains a member (r_brk)
  1020.     which is statically initialized at the time the shared library is
  1021.     built, to the offset of a function (_r_debug_state) which is guaran-
  1022.     teed to be called once before mapping in a library, and again when
  1023.     the mapping is complete.  At the time we are examining this member,
  1024.     it contains only the unrelocated offset of the function, so we have
  1025.     to do our own relocation.  Later, when the dynamic linker actually
  1026.     runs, it relocates r_brk to be the actual address of _r_debug_state().
  1027.  
  1028.     The debugger interface structure also contains an enumeration which
  1029.     is set to either RT_ADD or RT_DELETE prior to changing the mapping,
  1030.     depending upon whether or not the library is being mapped or unmapped,
  1031.     and then set to RT_CONSISTENT after the library is mapped/unmapped.
  1032. */
  1033.  
  1034. static int
  1035. enable_break ()
  1036. {
  1037.  
  1038.   int j;
  1039.  
  1040. #ifndef SVR4_SHARED_LIBS
  1041.  
  1042.   int in_debugger;
  1043.   
  1044.   /* Get link_dynamic structure */
  1045.  
  1046.   j = target_read_memory (debug_base, (char *) &dynamic_copy,
  1047.               sizeof (dynamic_copy));
  1048.   if (j)
  1049.     {
  1050.       /* unreadable */
  1051.       return (0);
  1052.     }
  1053.  
  1054.   /* Calc address of debugger interface structure */
  1055.  
  1056.   debug_addr = (CORE_ADDR) dynamic_copy.ldd;
  1057.  
  1058.   /* Calc address of `in_debugger' member of debugger interface structure */
  1059.  
  1060.   flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
  1061.                     (char *) &debug_copy);
  1062.  
  1063.   /* Write a value of 1 to this member.  */
  1064.  
  1065.   in_debugger = 1;
  1066.  
  1067.   write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
  1068.  
  1069. #else    /* SVR4_SHARED_LIBS */
  1070.  
  1071. #ifdef BKPT_AT_MAIN
  1072.  
  1073.   struct minimal_symbol *msymbol;
  1074.  
  1075.   msymbol = lookup_minimal_symbol ("main", symfile_objfile);
  1076.   if ((msymbol != NULL) && (msymbol -> address != 0))
  1077.     {
  1078.       breakpoint_addr = msymbol -> address;
  1079.     }
  1080.   else
  1081.     {
  1082.       return (0);
  1083.     }
  1084.  
  1085.   if (target_insert_breakpoint (breakpoint_addr, shadow_contents) != 0)
  1086.     {
  1087.       return (0);
  1088.     }
  1089.  
  1090. #else    /* !BKPT_AT_MAIN */
  1091.  
  1092.   struct symtab_and_line sal;
  1093.  
  1094.   /* Read the debugger interface structure directly. */
  1095.  
  1096.   read_memory (debug_base, (char *) &debug_copy, sizeof (debug_copy));
  1097.  
  1098.   /* Set breakpoint at the debugger interface stub routine that will
  1099.      be called just prior to each mapping change and again after the
  1100.      mapping change is complete.  Set up the (nonexistent) handler to
  1101.      deal with hitting these breakpoints.  (FIXME). */
  1102.  
  1103.   warning ("'%s': line %d: missing SVR4 support code", __FILE__, __LINE__);
  1104.  
  1105. #endif    /* BKPT_AT_MAIN */
  1106.  
  1107. #endif    /* !SVR4_SHARED_LIBS */
  1108.  
  1109.   return (1);
  1110. }
  1111.   
  1112. /*
  1113.   
  1114. GLOBAL FUNCTION
  1115.   
  1116.     solib_create_inferior_hook -- shared library startup support
  1117.   
  1118. SYNOPSIS
  1119.   
  1120.     void solib_create_inferior_hook()
  1121.   
  1122. DESCRIPTION
  1123.   
  1124.     When gdb starts up the inferior, it nurses it along (through the
  1125.     shell) until it is ready to execute it's first instruction.  At this
  1126.     point, this function gets called via expansion of the macro
  1127.     SOLIB_CREATE_INFERIOR_HOOK.
  1128.  
  1129.     For both SunOS shared libraries, and SVR4 shared libraries, we
  1130.     can arrange to cooperate with the dynamic linker to discover the
  1131.     names of shared libraries that are dynamically linked, and the
  1132.     base addresses to which they are linked.
  1133.  
  1134.     This function is responsible for discovering those names and
  1135.     addresses, and saving sufficient information about them to allow
  1136.     their symbols to be read at a later time.
  1137.  
  1138. FIXME
  1139.  
  1140.     Between enable_break() and disable_break(), this code does not
  1141.     properly handle hitting breakpoints which the user might have
  1142.     set in the startup code or in the dynamic linker itself.  Proper
  1143.     handling will probably have to wait until the implementation is
  1144.     changed to use the "breakpoint handler function" method.
  1145.  
  1146.     Also, what if child has exit()ed?  Must exit loop somehow.
  1147.   */
  1148.  
  1149. void 
  1150. solib_create_inferior_hook()
  1151. {
  1152.   
  1153.   if ((debug_base = locate_base ()) == 0)
  1154.     {
  1155.       /* Can't find the symbol or the executable is statically linked. */
  1156.       return;
  1157.     }
  1158.  
  1159.   if (!enable_break ())
  1160.     {
  1161.       warning ("shared library handler failed to enable breakpoint");
  1162.       return;
  1163.     }
  1164.  
  1165.   /* Now run the target.  It will eventually hit the breakpoint, at
  1166.      which point all of the libraries will have been mapped in and we
  1167.      can go groveling around in the dynamic linker structures to find
  1168.      out what we need to know about them. */
  1169.  
  1170.   clear_proceed_status ();
  1171.   stop_soon_quietly = 1;
  1172.   stop_signal = 0;
  1173.   do
  1174.     {
  1175.       target_resume (0, stop_signal);
  1176.       wait_for_inferior ();
  1177.     }
  1178.   while (stop_signal != SIGTRAP);
  1179.   stop_soon_quietly = 0;
  1180.   
  1181.   /* We are now either at the "mapping complete" breakpoint (or somewhere
  1182.      else, a condition we aren't prepared to deal with anyway), so adjust
  1183.      the PC as necessary after a breakpoint, disable the breakpoint, and
  1184.      add any shared libraries that were mapped in. */
  1185.  
  1186.   if (DECR_PC_AFTER_BREAK)
  1187.     {
  1188.       stop_pc -= DECR_PC_AFTER_BREAK;
  1189.       write_register (PC_REGNUM, stop_pc);
  1190.     }
  1191.  
  1192.   if (!disable_break ())
  1193.     {
  1194.       warning ("shared library handler failed to disable breakpoint");
  1195.     }
  1196.  
  1197.   solib_add ((char *) 0, 0, (struct target_ops *) 0);
  1198. }
  1199.  
  1200. /*
  1201.  
  1202. LOCAL FUNCTION
  1203.  
  1204.     special_symbol_handling -- additional shared library symbol handling
  1205.  
  1206. SYNOPSIS
  1207.  
  1208.     void special_symbol_handling (struct so_list *so)
  1209.  
  1210. DESCRIPTION
  1211.  
  1212.     Once the symbols from a shared object have been loaded in the usual
  1213.     way, we are called to do any system specific symbol handling that 
  1214.     is needed.
  1215.  
  1216.     For Suns, this consists of grunging around in the dynamic linkers
  1217.     structures to find symbol definitions for "common" symbols and 
  1218.     adding them to the minimal symbol table for the corresponding
  1219.     objfile.
  1220.  
  1221. */
  1222.  
  1223. static void
  1224. special_symbol_handling (so)
  1225. struct so_list *so;
  1226. {
  1227. #ifndef SVR4_SHARED_LIBS
  1228.  
  1229.   /* Read the debugger structure from the inferior, just to make sure
  1230.      we have a current copy. */
  1231.  
  1232.   read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
  1233.  
  1234.   /* Get common symbol definitions for the loaded object. */
  1235.  
  1236.   if (debug_copy.ldd_cp)
  1237.     {
  1238.       solib_add_common_symbols (debug_copy.ldd_cp, so -> objfile);
  1239.     }
  1240.  
  1241. #endif    /* !SVR4_SHARED_LIBS */
  1242. }
  1243.  
  1244.  
  1245. /*
  1246.  
  1247. LOCAL FUNCTION
  1248.  
  1249.     sharedlibrary_command -- handle command to explicitly add library
  1250.  
  1251. SYNOPSIS
  1252.  
  1253.     static void sharedlibrary_command (char *args, int from_tty)
  1254.  
  1255. DESCRIPTION
  1256.  
  1257. */
  1258.  
  1259. static void
  1260. sharedlibrary_command (args, from_tty)
  1261. char *args;
  1262. int from_tty;
  1263. {
  1264.   dont_repeat ();
  1265.   solib_add (args, from_tty, (struct target_ops *) 0);
  1266. }
  1267.  
  1268. void
  1269. _initialize_solib()
  1270. {
  1271.   
  1272.   add_com ("sharedlibrary", class_files, sharedlibrary_command,
  1273.        "Load shared object library symbols for files matching REGEXP.");
  1274.   add_info ("sharedlibrary", info_sharedlibrary_command, 
  1275.         "Status of loaded shared object libraries.");
  1276. }
  1277. @
  1278.